package com.GlobalPaint.app.Adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.GlobalPaint.app.R;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import cn.finalteam.galleryfinal.model.PhotoInfo;

/**
 * ================================================
 * 作    者：ikkong （ikkong@163.com），修改 jeasonlzy（廖子尧）
 * 版    本：1.0
 * 创建日期：2016/5/19
 * 描    述：
 * 修订历史：微信图片选择的Adapter, 感谢 ikkong 的提交
 * ================================================
 */
public class ImagePickerAdapter_xunwu extends RecyclerView.Adapter<ImagePickerAdapter_xunwu.SelectedPicViewHolder> {
    private int maxImgCount;
    private int actImgCount;
    private Context mContext;
    private List<PhotoInfo> mData;
    private LayoutInflater mInflater;
    private OnRecyclerViewItemClickListener listener;
    private boolean isAdded;   //是否额外添加了最后一个图片

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.listener = listener;
    }

    public void setImages(List<PhotoInfo> data) {
        actImgCount = data.size();
        mData = new ArrayList<>(data);
        if (getItemCount() < maxImgCount) {
            mData.add(new PhotoInfo());
            isAdded = true;
        } else {
            isAdded = false;
        }
        notifyDataSetChanged();
    }

    public List<PhotoInfo> getImages() {
        //由于图片未选满时，最后一张显示添加图片，因此这个方法返回真正的已选图片
        if (isAdded) return new ArrayList<>(mData.subList(0, mData.size() - 1));
        else return mData;
    }

    public ImagePickerAdapter_xunwu(Context mContext, List<PhotoInfo> data, int maxImgCount) {
        this.mContext = mContext;
        this.maxImgCount = maxImgCount;
        this.actImgCount = data.size();
        this.mInflater = LayoutInflater.from(mContext);
        setImages(data);
    }
    @Override
    public SelectedPicViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new SelectedPicViewHolder(mInflater.inflate(R.layout.list_item_image_xunwu, parent, false));
    }

    @Override
    public void onBindViewHolder(SelectedPicViewHolder holder, int position) {
        holder.bind(position);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class SelectedPicViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private ImageView iv_img;
        private ImageView iv_delete;
        private int clickPosition;

        public SelectedPicViewHolder(View itemView) {
            super(itemView);
            iv_img = (ImageView) itemView.findViewById(R.id.iv_img);
            iv_delete = (ImageView) itemView.findViewById(R.id.iv_delete);
        }

        public void bind(final int position) {
            //设置条目的点击事件
            itemView.setOnClickListener(this);
            //根据条目位置设置图片
            PhotoInfo item = mData.get(position);
            if (isAdded && position == getItemCount() - 1) {
                iv_img.setImageResource(R.drawable.selector_image_add1);
                iv_delete.setVisibility(View.GONE);
                clickPosition = -1;
            } else {
                ImageLoader.getInstance().displayImage("file:/" + item.getPhotoPath(), iv_img);
                iv_delete.setVisibility(View.VISIBLE);
                iv_delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mData.remove(position);

                        iv_delete.setVisibility(View.GONE);
                        actImgCount--;
                        if (actImgCount == maxImgCount - 1) {
                            mData.add(new PhotoInfo());
                            isAdded = true;
                        }
                        //如果图片全部删除，增加一个选择图片
                       /* if(mData.size()==0){
                            mData.add(new PhotoInfo());
                            isAdded = true;
                        }*/
                        notifyDataSetChanged();

                    }

                });
                clickPosition = position;
            }

        }

        @Override
        public void onClick(View v) {
            if (listener != null) listener.onItemClick(v, clickPosition);
        }
    }
}